import React, { Component } from 'react';
import './index.less';
import { base } from 'nc-lightapp-front';
import checkEditData from './functions/checkEditData';
import saveEditData from './functions/saveEditData';
import BatchEditComp from './components/BatchEditComp';

const { NCModal, NCButton } = base;
const { Header: NCModalHeader, Body: NCModalBody, Footer: NCModalFooter } = NCModal;

class BatchEditModal extends Component {
	constructor(props) {
		super(props);
		this.getEditInitInfo = this.getEditInitInfo.bind(this);
		this.closeModal = this.closeModal.bind(this);
		this.beSure = this.beSure.bind(this);
		this.saveEditData = this.saveEditData.bind(this);
		this.warningCloseHandle = this.warningCloseHandle.bind(this);
		this.confirmSureHandle = this.confirmSureHandle.bind(this);
		this.confirmCloseHandle = this.confirmCloseHandle.bind(this);
	}

	getEditInitInfo() {
		/*const {isInJob, orgVal, table, func_type} = this.props;

        const param_str = table.getCheckedRows('info').map(item => item.data.values.pk_psnjob.value).join();
        if (!param_str) return;
        getEditInitData({
            func_type,
            isInJob,
            param_str,
            pk_org: orgVal.refpk
        })
            .then((res) => {
                if (res.success) {
                }
            });*/
	}

	isEmpty(param) {
		let hasvalue = false;
		for (let pro in param) {
			hasvalue = true;
			break;
		}
		return !hasvalue;
	}

	beSure() {
		const editInfo = this.batchEditNode.getInfo();
		if (this.isEmpty(editInfo)) {
			this.closeModal();
		} else {
			const { orgVal, table } = this.props;
			const param_str = table.getCheckedRows('info').map((item) => item.data.values.pk_psnjob.value).join();
			if (!param_str) return;
			checkEditData({
				edit_value: JSON.stringify(editInfo),
				table_code: 'hi_psndoc_part',
				param_str,
				pk_org: orgVal.refpk
			}).then((res) => {
				if (res.success) {
					this.editInfo = editInfo;
					this.passPks = res.data.passPks;
					this.confirmMsg = res.data.confirmMsg;
					this.confirmPks = res.data.confirmPks;
					this.worningMsg = res.data.worningMsg;
					if (!this.confirmMsg && !this.worningMsg) {
						this.saveEditData();
					} else {
						this.props.modal.show(this.worningMsg ? 'warningModal' : 'confirmModal');
					}
				}
			});
		}
	}

	saveEditData() {
		if (!this.passPks) {
			this.closeModal();
			this.props.getMainTableData(true);
			return;
		}
		saveEditData({
			edit_value: JSON.stringify(this.editInfo),
			table_code: 'hi_psndoc_part',
			param_str: this.passPks,
			pk_org: this.props.orgVal.refpk
		}).then((res) => {
			if (res.success) {
				this.closeModal();
				this.props.getMainTableData(true);
			}
		});
	}

	warningCloseHandle() {
		const { modal } = this.props;
		modal.close('warningModal');
		if (!this.confirmMsg) {
			this.saveEditData();
		} else {
			modal.show('confirmModal');
		}
	}

	confirmSureHandle() {
		const { modal } = this.props;
		modal.close('confirmModal');
		if (this.passPks) {
			this.passPks = this.passPks.concat(',' + this.confirmPks);
		} else {
			this.passPks = this.confirmPks;
		}
		this.saveEditData();
	}

	confirmCloseHandle() {
		const { modal } = this.props;
		modal.close('confirmModal');
		this.saveEditData();
	}

	closeModal() {
		this.props.onClose && this.props.onClose();
	}

	render() {
		const { visible, modal } = this.props;
		const { createModal } = modal;
		return (
			<div>
				<NCModal show={visible} onHide={this.closeModal} onEntered={this.getEditInitInfo} backdrop={'static'}>
					<NCModalHeader closeButton={true}>
						{this.props.json['hi6007-000040']}
						{/* 国际化处理： 批量修改*/}
					</NCModalHeader>
					<NCModalBody>
						<BatchEditComp ref={(node) => (this.batchEditNode = node)} json={this.props.json} />
					</NCModalBody>
					<NCModalFooter>
						<NCButton colors="primary" onClick={this.beSure}>
							{this.props.json['hi6007-000174']} {/* 国际化处理： 确认*/}
						</NCButton>
						<NCButton shape="border" onClick={this.closeModal}>
							{this.props.json['hi6007-000005']}
							{/* 国际化处理： 取消*/}
						</NCButton>
					</NCModalFooter>
				</NCModal>
				{createModal('warningModal', {
					title: `${this.props.json['hi6007-000108']}` /* 国际化处理： 提醒*/,
					content: (
						<div
							dangerouslySetInnerHTML={{
								__html: this.worningMsg
							}}
						/>
					),
					size: 'sm',
					backdrop: 'static',
					noFooter: true,
					userControl: true,
					cancelBtnClick: this.warningCloseHandle
				})}

				{createModal('confirmModal', {
					title: this.props.json['hi6007-000174'] /* 国际化处理： 确认,确认*/,
					content: (
						<div
							dangerouslySetInnerHTML={{
								__html: this.confirmMsg
							}}
						/>
					),
					beSureBtnClick: this.confirmSureHandle,
					size: 'sm',
					backdrop: 'static',
					rightBtnName: this.props.json['hi6007-000005'], //左侧按钮名称,默认关闭/* 国际化处理： 取消*/
					leftBtnName: this.props.json['hi6007-000004'], //右侧按钮名称， 默认确认/* 国际化处理： 确定*/
					userControl: true,
					cancelBtnClick: this.confirmCloseHandle
				})}
			</div>
		);
	}
}

export default BatchEditModal;
